/*
 * Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
 * This file is part of SIGAR.
 * 
 * SIGAR is free software; you can redistribute it and/or modify
 * it under the terms version 2 of the GNU General Public License as
 * published by the Free Software Foundation. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA.
 */
package physicalmachine;

import java.lang.management.ManagementFactory;
import org.hyperic.sigar.Cpu;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.cmd.Shell;
import org.hyperic.sigar.cmd.SigarCommandBase;
import static utils.Constants.*;

/**
 * Responsible for obtaining local CPU information
 */
public class CPU extends SigarCommandBase {

    private String cPUModel;
    private String cPUVendor;
    private int cPUCores;
    private long cPUMhz;
    private String cPUidle;
    private String cPuUsed;
    private long cPuUserTime;
    private long cPuTotalTime;
    private long cPuIdleTime;

    public CPU(Shell shell) {
        super(shell);
    }

    public CPU() {
        super();
    }

    /**
     * Responsible for obtaining and setting the available CPU cores
     * @return
     */
    private void getAvailableCPUCores() {
        com.sun.management.OperatingSystemMXBean mxbean = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        cPUCores = mxbean.getAvailableProcessors();
    }

    /**
     * Responsible for obtaining and setting the total idle CPU percentage
     * @param cpu
     */
    private void getIdleCPU(CpuPerc cpu) {
        cPUidle = CpuPerc.format(cpu.getIdle());
        cPUidle=cPUidle.replace("%","").trim();
        cPuUsed = String.valueOf(100-(Double.parseDouble(cPUidle))).toString();
    }

    public void getCPUTimes(Cpu cpu1) {
        cPuUserTime= cpu1.getUser();
        //System.out.println(cPuUserTime);
        cPuTotalTime=cpu1.getTotal();
        //System.out.println(cPuTotalTime);
        cPuIdleTime=cpu1.getIdle();
        //System.out.println(cPuIdleTime);
        //System.out.println(cPUidle);
    }

    /**
     * Responsible for obtaining and setting the Vendor, Model and Mhz CPU information
     * @param args
     * @throws SigarException
     */
    public void output(String[] args) throws SigarException {
        org.hyperic.sigar.CpuInfo[] infos = this.sigar.getCpuInfoList();
        org.hyperic.sigar.CpuInfo info = infos[0];
        cPUVendor = info.getVendor();
        cPUModel = info.getModel();
        cPUMhz = info.getMhz();


    }

    public static void main(String[] args) throws Exception {
        CPU cpu = new CPU();
        cpu.getCPUinfo();

    }

    /**
     * Responsible for setting all the local CPU information
     */
    public void getCPUinfo() {
        try {
            getIdleCPU(this.sigar.getCpuPerc());
            getAvailableCPUCores();
            getCPUTimes(this.sigar.getCpu());
            output(new String[0]);
        } catch (SigarException ex) {
            System.err.println(ERROR_MESSAGE + "getting the CPU information: " + ex.getMessage());
        }
    }

    /**
     * @return the CPUidle
     */
    public String getCPUidle() {
        return cPUidle;
    }

    /**
     * @return the CPUVendor
     */
    public String getCPUVendor() {
        return cPUVendor;
    }

    /**
     * @return the CPUModel
     */
    public String getCPUModel() {
        return cPUModel;
    }

    /**
     * @return the CPUMhz
     */
    public String getCPUMhz() {
        return cPUMhz + " Mhz";
    }

    /**
     * @return the CPUCores
     */
    public int getCPUCores() {
        return cPUCores;
    }

    /**
     * @return the cPuUsed
     */
    public String getCPUUsed() {
        return cPuUsed;
    }
    //retorna CPUUserTime
    public Long getCPUUserTime() {
        return cPuUserTime;
    }

    //retorna CPUUserTime
    public Long getCPUTotalTime() {
        return cPuTotalTime;
    }
}//end of CPU

